#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/wait.h>

#define MAXLINE 128


int find_device()
{
	char result_buf[MAXLINE];
	FILE *pp;
	char * p = NULL;
	char name[4] = {0};

	if( (pp = popen("fdisk -l", "r")) == NULL ) {
		printf("popen() error!/n");
		return -1;
	}

	while(fgets(result_buf, sizeof result_buf, pp));

	printf("----------------------------------\n");
	printf("buf = %s\n", result_buf);

	if ('\n' == result_buf[strlen(result_buf) - 1]) {
        result_buf[strlen(result_buf) - 1] = '\0';
    }

	printf("=================================\n");
	printf("buf = %s\n", result_buf);

	p = strstr(result_buf, "sd");
	if (p != NULL) {
		printf("p = %s\n", p);
		strncpy(name, p, 3);
		printf("name = %s\n", name);
	}
	else {
		return -1;
	}

	pclose(pp);
	return 0;
}

int main(int argc, char *argv[])
{
	while (1) {
		find_device();
		sleep(1);
	}

	return 0;
}

